package com.cskaoyan.wordmemorize.controller.app;

import com.alibaba.excel.annotation.format.DateTimeFormat;
import com.cskaoyan.wordmemorize.auth.StpKit;
import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.app.UserCheckinDTO;
import com.cskaoyan.wordmemorize.dto.app.UserCheckinStatisticsDTO;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.request.UserCheckinCommand;
import com.cskaoyan.wordmemorize.service.UserCheckinService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;

@RestController
@RequestMapping("user/checkin")
public class CheckinController {

    @Resource
    UserCheckinService userCheckinService;
    //同步用户打卡数据
    @PostMapping()
    public Result<UserCheckinStatisticsDTO> syncCheckinData(@RequestBody UserCheckinCommand userCheckinCommand) {
            // 设置当前用户ID
            userCheckinCommand.setUserId(StpKit.USER.getLoginIdAsString());
            UserCheckinStatisticsDTO dto = userCheckinService.checkin(userCheckinCommand);
            //获取打卡日期
            LocalDate signDate = userCheckinCommand.getSignDate();
            //判断打卡日期是今天之前还是今天之后
            if (!signDate.equals(LocalDate.now())) {
                 dto = userCheckinService.getUserCheckinStatistics(StpKit.USER.getLoginIdAsLong());
                 return Result.ok(dto).message("打卡日期不能是今天之后的日期");
            }
            //若是今天之前
            if (signDate.isBefore(LocalDate.now())) {
                //获取当前用户的打卡统计信息
                dto = userCheckinService.getUserCheckinStatistics(StpKit.USER.getLoginIdAsLong());
                return Result.ok(dto).message("打卡日期不能是今天之前的日期");
            }

            if (dto.getId() ==  null) {
                //获取当前用户的打卡统计信息
                dto = userCheckinService.getUserCheckinStatistics(StpKit.USER.getLoginIdAsLong());
                return Result.ok(dto). message("打卡错误");
            }
            return Result.ok(dto);
    }


    //下载用户的所有打卡数据
    @GetMapping("list")
    public Result<PageDTO<UserCheckinDTO>> downloadAllCheckinData(PageRequest pageRequest) {
        PageDTO<UserCheckinDTO> checkInData = userCheckinService.getCheckInData(pageRequest, StpKit.USER.getLoginIdAsLong());
        return Result.ok(checkInData);
    }
    //获取用户打卡数据统计
    @GetMapping("statistics")
    public Result<UserCheckinStatisticsDTO> getUserCheckinStatistics() {
        Long userId = StpKit.USER.getLoginIdAsLong();
        UserCheckinStatisticsDTO statisticsDTO = userCheckinService.getUserCheckinStatistics(userId);
        return Result.ok(statisticsDTO);
    }
    //使用补签卡
    @PutMapping("card")
    public Result useSupplementCard() {
        //判断补签的是那一天，若点击今天之后的就不能用，今天之前的日期
        //就能用，用完后，更新等级和单词上限。补签卡数量变为0。
        int i = userCheckinService.userResignCard(StpKit.USER.getLoginIdAsLong());
         if (i == 1) {
            return Result.ok().message("补签成功");
        }
          return Result.fail().message("补签失败");
    }
}
